import { LOGIN_SUCCESS, LOGOUT_SUCCESS,
  //  GEN_ACCESS_ROUTES 
  } 
   from "../actions/action-type"

   import { generateAccessRoutes } from "../untils/permission"

let menus = JSON.parse(localStorage.getItem('menus') || sessionStorage.getItem('menus')) || []
// 生成前端路由数组
menus = generateAccessRoutes(menus)


const initialState = {
  name: localStorage.getItem('name') || sessionStorage.getItem('name') || '',
  avatarUrl: localStorage.getItem('avatarUrl') || sessionStorage.getItem('avatarUrl') || '',
  token: localStorage.getItem('token') || sessionStorage.getItem('token') || '',
  menus,
  stuNo:localStorage.getItem('stuNo') || sessionStorage.getItem('stuNo') || ''
}

const reducer = (state = initialState, { type, payload }) => {
  switch (type) {
    case LOGIN_SUCCESS: // 登录成功，合并状态数据
      return { ...state, ...payload }
    // case GEN_ACCESS_ROUTES: // 生成访问路由
    //   return {
    //     ...state,
    //     ...payload
    //   }
    case LOGOUT_SUCCESS: // 退出成功
      return {
        name: '',
        avatarUrl: '',
        token: '',
        menus: []
      }
    default:
      return state
  }
}

export default reducer

